#include "control/user_control.h"
#include "crypt/crypt_7th.h"
#include "model/model_dir.h"
#include "our_header.h"
#include "service/feature_7th.h"
#include "tools/my_tlv.h"
#include "tpool/threadspool.h"
#include <stdio.h>
#include <string.h>
#include <strings.h>

int loginControl(int fd, MYSQL *conn, char username[]) {
	USER_M user = {0};
	int tag = selectUserByName(conn, username, &user);

	train_t data = {0};

	if(tag) {
		data.cmdstat = LOGIN | RES;
		data.length = strlen("no user");
		strcpy(data.data, "no user");
		send_train(fd, &data);
		return 0;
	}

	data.cmdstat = LOGIN | HASH | IS_OK;
	data.length = strlen(user.salt);
	strcpy(data.data, user.salt);
	send_train(fd, &data);
	bzero(&data, sizeof(data));

	if(recv_train(fd, &data)) {
		printf("recv_train error maybe close\n");
		return -1;
	}

	if(strcmp(data.data, user.passwd)) {
		data.cmdstat = LOGIN | RES;
		data.length = strlen("passwd error");
		strcpy(data.data, "passwd error");
		send_train(fd, &data);
		return 0;
	}

	data.cmdstat = LOGIN | IS_OK | TOKEN;

	extra_data_t ex_data = {0};
	getToken(user.name, ex_data.token);
	ex_data.cur_dir_id = user.last_dir_id;
	memcpy(ex_data.user, user.name, strlen(user.name));

	data.length = sizeof(extra_data_t);
	memcpy(data.data, &ex_data, sizeof(extra_data_t));
	send_train(fd, &data);
	return 0;
}

int regisControl(int fd, MYSQL *conn, char username[]) {
	USER_M user = {0};
	int tag = selectUserByName(conn, username, &user);

	train_t data = {0};

	if(tag==0) {
		data.cmdstat = RIGIST | RES;
		data.length = strlen("用户名重复");
		strcpy(data.data, "用户名重复");
		send_train(fd, &data);
		return 0;
	}

	data.cmdstat = RIGIST | HASH | IS_OK;
	char *salt;
	getRandCryptSalt(&salt);
	data.length = strlen(salt);
	strcpy(data.data, salt);
	send_train(fd, &data);

	bzero(&data, sizeof(data));

	if(recv_train(fd, &data)) {
		printf("recv_train error maybe close\n");
		return -1;
	}
	int cur_dir;
	if(registUser(conn, username, salt, data.data, &cur_dir)) {
		printf("registUser error\n");
		return -1;
	};
	data.cmdstat = LOGIN | IS_OK | TOKEN;

	extra_data_t ex_data = {0};
	getToken(username, ex_data.token);
	ex_data.cur_dir_id = cur_dir;
	memcpy(ex_data.user, username, strlen(username));

	data.length = sizeof(extra_data_t);
	memcpy(data.data, &ex_data, sizeof(extra_data_t));
	send_train(fd, &data);
	return 0;
}

int dirControl(int fd, MYSQL *conn, train_t *tdata_get) {
	if(checkToken(tdata_get)) {
		bzero(tdata_get->data + sizeof(extra_data_t), sizeof(tdata_t) - sizeof(extra_data_t) - sizeof(cmdstat_t) - sizeof(int));
		char sentec[] = "token 失效";
		strcpy(tdata_get->data + sizeof(extra_data_t), sentec);
		tdata_get->length = sizeof(extra_data_t) + strlen(sentec);
		send_train(fd, tdata_get);
		return -1;
	}
	extra_data_t *exdata = (extra_data_t *)tdata_get->data;
	char despath[124] = {0};

	dir_list_t lists;

	int dest_dir_id;
	if(gotoDir(conn, exdata->cur_dir_id, tdata_get->data + sizeof(extra_data_t), &dest_dir_id, &lists)) {
		bzero(tdata_get->data + sizeof(extra_data_t), sizeof(tdata_t) - sizeof(extra_data_t) - sizeof(cmdstat_t) - sizeof(int));
		tdata_get->cmdstat = tdata_get->cmdstat | RES;
		char sentec[] = "目录不存在";
		strcpy(tdata_get->data + sizeof(extra_data_t), sentec);
		tdata_get->length = sizeof(extra_data_t) + strlen(sentec);
		send_train(fd, tdata_get);
		return 0;
	};

	// cd 的结果
	train_t dadd={0};

	dadd.cmdstat =CD | IS_OK;
	exdata->cur_dir_id = dest_dir_id;
	memcpy(dadd.data,exdata,sizeof(extra_data_t));

	DIR_M dir = {0};
	selectDirByid(conn, dest_dir_id, &dir);
	strcpy(dadd.data + sizeof(extra_data_t), dir.path);
	dadd.length = sizeof(extra_data_t) + strlen(dir.path);
	send_train(fd, &dadd);

	// ls结果
	// train_t response = {0};
	// response.cmdstat = LS | RES | IS_OK;
	// memcpy(response.data, exdata, sizeof(extra_data_t));
	// memcpy((char *)response.data + sizeof(extra_data_t), // 关键点：计算追加偏移量
	// 	   &lists.size,									 // 注意取地址符&
	// 	   sizeof(lists.size));
	// memcpy((char *)response.data + sizeof(extra_data_t) + sizeof(lists.size), lists.item_head, sizeof(dir_item_t) * lists.size);
	// response.length = sizeof(cmdstat_t) + sizeof(int) + sizeof(extra_data_t) + sizeof(lists.size) + sizeof(dir_item_t) * lists.size;
	// printf("lists len:%d\n", response.length);
	// send_train(fd, tdata_get);
	return 0;
}